package com.greateffect.littlebud.lib.utils;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import com.greateffect.littlebud.lib.cache.MMKVUtils;

import static android.content.Context.TELEPHONY_SERVICE;

/**
 * Created by ZengCS on 2017/7/4.
 * E-mail:zcs@sxw.cn
 * Add:成都市天府软件园E3-3F
 */

public class AndroidUtils {
    public static String getDeviceDetailInfo(Context context) {
        try {
            StringBuilder sb = new StringBuilder();
            sb.append("MODEL:");
            sb.append(Build.MODEL);
            sb.append("\nAndroid OS:");
            sb.append(Build.VERSION.RELEASE);
            sb.append("[Level ");
            sb.append(Build.VERSION.SDK_INT);
            sb.append("]");
            sb.append("\nIMEI:");
            sb.append(getDeviceId(context));
            sb.append("\nMAC:");
            sb.append(MacUtil.getLocalMacAddressFromIp());
            sb.append("\nClientID:\n");
            sb.append(MMKVUtils.getString(SPKeys.Account.PUSH_CLIENT_ID, ""));
            return sb.toString();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取当前设备IMEI
     */
    public static String getDeviceId(Context context) {
        // 引入新的获取ID方式
        try {
            String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            JLogUtil.d("androidId = " + androidId);
            if (!TextUtils.isEmpty(androidId)) {
                return androidId;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        TelephonyManager tm = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            return "000000000000000";
        }
        String imei = tm.getDeviceId();
        if (!TextUtils.isEmpty(imei)) {
            return imei;
        }
        return "000000000000000";
    }

    /**
     * 获取应用版本号
     */
    public static int getAppVersionCode(Context context) {
        int versionCode = 0;
        try {
            // ---get the package info---
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionCode = pi.versionCode;
        } catch (Exception e) {
            Log.e("VersionInfo", "Exception", e);
        }
        return versionCode;
    }

    /**
     * 获取应用版本名
     */
    public static String getAppVersionName(Context context) {
        String versionName = "";
        try {
            // ---get the package info---
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
            if (versionName == null || versionName.length() <= 0) {
                return "";
            }
        } catch (Exception e) {
            Log.e("VersionInfo", "Exception", e);
        }
        return versionName;
    }
}
